package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.DimensionDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.DimensionInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchDimensionInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IDimensionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DimensionServiceImpl implements IDimensionService {
    @Autowired
    private DimensionDao dao;
    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<DimensionInfo> selectPageListByQueryGetMap(Page<DimensionInfo> page, SearchDimensionInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getDimensionName())) {
            params.put("dimensionName", searchInfo.getDimensionName());
        }
        return dao.getQueryPage(page, params);
    }

    public DimensionInfo selectOneById(String dimensionId) {
        String sql = "select * from dimension_info where id= '" + dimensionId + "'";
        DimensionInfo info = sqlMapper.selectOne(sql, DimensionInfo.class);
        return info;
    }

    public int insert(DimensionInfo info) {
        String sql = "";
        sql = "INSERT INTO `dimension_info` (`dimension_china_name`, `dimension_english_name`, `num`,`remarks`) VALUES" +
                "('" + info.getDimensionChinaName().trim() + "','" + info.getDimensionEnglishName().trim() + "'," +
                " '" + info.getNum() + "','" + info.getRemarks().trim() + "');";
        return sqlMapper.insert(sql);
    }

    @Transactional
    public void delete(String id) {
        String sql = "delete from dimension_info where id = '" + id + "'";
        sqlMapper.delete(sql);
    }

    public int update(DimensionInfo info) {
        String sql = "update dimension_info set dimension_china_name='" + info.getDimensionChinaName().trim() + "'," +
                " dimension_english_name='"+info.getDimensionEnglishName()+"',num='"+info.getNum()+"'" +
                " ,remarks='"+info.getRemarks()+"' where id = '" + info.getId() + "';";
        return sqlMapper.update(sql);
    }

    public List<Map<String, Object>> list() {
        String sql = "select id, dimension_china_name as dimensionChinaName," +
                "dimension_english_name as dimensionEnglishName from dimension_info order by num;";
        return sqlMapper.selectList(sql);
    }

    public List<Map<String, Object>> noDepartmentList() {
        String sql = "select id, dimension_china_name as dimensionChinaName," +
                " dimension_english_name as dimensionEnglishName from dimension_info " +
                " where dimension_china_name!='科室' order by num;";
        return sqlMapper.selectList(sql);
    }
}
